package com.miracle9.game.dao;
import com.miracle9.game.entity.ComputerOrder;
import com.miracle9.game.util.MyUtil;
import java.util.Date;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

@Repository
public class ComputerOrderServiceDao extends SqlOperating<ComputerOrder, Integer>
{
  public void deleteById(int userId)
  {
	  q_CreateSql("delete from ComputerOrder where user.id=?", new Object[] { Integer.valueOf(userId) }).executeUpdate();
  }
  
  public void deletePastOrder(String date)
  {
	  q_CreateSql("delete from ComputerOrder where borrowDate<?", new Object[] { date }).executeUpdate();
  }
  
  public int getSumDepositByBorrowDate(String date)
  {
    String hql = "select sum(deposit) from ComputerOrder where borrowDate like ? and user.promoterId=0";
    Object sum = q_CreateSql(hql, new Object[] { date + "%" }).uniqueResult();
    if (sum == null) {
      return 0;
    }
    return ((Number)sum).intValue();
  }
  
  public int getSumDepositByRepayDate(String date)
  {
    String hql = "select sum(returnDeposit) from ComputerOrder where repayDate like ? and user.promoterId=0";
    Object sum = q_CreateSql(hql, new Object[] { date + "%" }).uniqueResult();
    if (sum == null) {
      return 0;
    }
    return ((Number)sum).intValue();
  }
  
  public int getSumDepositByBorrowDate(String date, String admin)
  {
    String hql = "select sum(deposit) from ComputerOrder where borrowDate like ? and admin = ?";
    Object sum = q_CreateSql(hql, new Object[] { date + "%", admin }).uniqueResult();
    if (sum == null) {
      return 0;
    }
    return ((Number)sum).intValue();
  }
  
  public int getSumReturnDepositByRepayDate(String date, String admin)
  {
    String hql = "select sum(returnDeposit) from ComputerOrder where repayDate like ? and repayAdmin = ?";
    Object sum = q_CreateSql(hql, new Object[] { date + "%", admin }).uniqueResult();
    if (sum == null) {
      return 0;
    }
    return ((Number)sum).intValue();
  }
  
  public void updates(String date)
  {
	  q_CreateSql("update ComputerOrder set deposit = 0 where borrowDate like ?", new Object[] { date + "%" }).executeUpdate();
	  q_CreateSql("update ComputerOrder set returnDeposit = 0 where repayDate like ?", new Object[] { date + "%" }).executeUpdate();
  }
  
  public boolean updateReturnInfoByIdAndstatus_0(int userId, int returnDeposit, String repayAdmin, String remark)
  {
    int cnt = q_CreateSql("update ComputerOrder set returnDeposit = ?,repayRemark=?,repayDate=?,repayAdmin=?,status=1 where user.id=? and status=0", new Object[] {
      Integer.valueOf(returnDeposit), remark, MyUtil.dateFormatYMD_HMS_Sting(new Date()), repayAdmin, Integer.valueOf(userId) }).executeUpdate();
    if (cnt > 0) {
      return true;
    }
    return false;
  }
  
  public int getQuantityByUidAndStatus_0(int userId)
  {
    String hql = "select count(*) from ComputerOrder where user.id = ? and status = 0";
    Object sum = q_CreateSql(hql, new Object[] { Integer.valueOf(userId) }).uniqueResult();
    if (sum == null) {
      return 0;
    }
    return ((Number)sum).intValue();
  }
}
